package cn.scw.project.goods.service.impl;

import cn.scw.common.exception.CustomException;
import cn.scw.common.utils.SecurityUtils;
import cn.scw.common.utils.bean.ToolUtils;
import cn.scw.common.utils.sql.JpaUtil;
import cn.scw.project.goods.domain.GCoupon;
import cn.scw.project.goods.domain.GCouponUser;
import cn.scw.project.goods.domain.vo.GCouponUserVo;
import cn.scw.project.goods.mapper.GCouponUserMapper;
import cn.scw.project.goods.service.IGCouponService;
import cn.scw.project.goods.service.IGCouponUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 用户优惠券Service业务层处理
 *
 * @author api
 * @date 2020-05-20
 */
@Service
public class GCouponUserServiceImpl implements IGCouponUserService {
    @Autowired
    private GCouponUserMapper gCouponUserMapper;
    @Autowired
    private IGCouponService gCouponService;
    @Autowired
    private IGCouponUserService iGCouponUserService;


    /**
     * 查询用户优惠券
     *
     * @param id 用户优惠券ID
     * @return 用户优惠券
     */
    @Override
    public GCouponUser selectGCouponUserById(Long id) {
        return gCouponUserMapper.selectGCouponUserById(id);
    }

    @Override
    public GCouponUserVo selectGCouponUserVoById(Long id) {
        return gCouponUserMapper.selectGCouponUserVoById(id);
    }

    /**
     * 查询用户优惠券列表
     *
     * @param statusCode 用户优惠券
     * @return 用户优惠券
     */
    @Override
    public List<GCouponUserVo> selectGCouponUserList(String statusCode) {
        return gCouponUserMapper.selectGCouponUserList(JpaUtil.getEqualOrAll(statusCode));
    }

    /**
     * 新增用户优惠券
     *
     * @param gCouponUser 用户优惠券
     * @return 结果
     */
    @Override
    public int insertGCouponUser(GCouponUser gCouponUser) {
        return gCouponUserMapper.insertGCouponUser(gCouponUser);
    }

    /**
     * 修改用户优惠券
     *
     * @param gCouponUser 用户优惠券
     * @return 结果
     */
    @Override
    public int updateGCouponUser(GCouponUser gCouponUser) {
        return gCouponUserMapper.updateGCouponUser(gCouponUser);
    }

    /**
     * 批量删除用户优惠券
     *
     * @param ids 需要删除的用户优惠券ID
     * @return 结果
     */
    @Override
    public int deleteGCouponUserByIds(Long[] ids) {
        return gCouponUserMapper.deleteGCouponUserByIds(ids);
    }

    /**
     * 删除用户优惠券信息
     *
     * @param id 用户优惠券ID
     * @return 结果
     */
    @Override
    public int deleteGCouponUserById(Long id) {
        return gCouponUserMapper.deleteGCouponUserById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void receive(Long[] ids) {
        int row = gCouponService.updateGCouponResidue(ids);
        if (row < ids.length)
            throw new CustomException("优惠券不存在");


        // 检查优惠券是否领取过
        for (Long id : ids) {
            GCouponUser couponUser = iGCouponUserService.selectGCouponUserById(id);
            if(couponUser != null) {
                throw new CustomException("优惠券已存在，不能领取了");
            }
        }

        for (Long id : ids) {
            GCoupon gCoupon = gCouponService.selectGCouponById(id);
            GCouponUser gCouponUser = new GCouponUser(id, SecurityUtils.getLoginUser().getUser().getUserId(), gCoupon.getUseScopeCode());
            gCouponUserMapper.insertGCouponUser(gCouponUser);
        }
    }

    @Override
    public void updateStatus() {
        this.gCouponUserMapper.updateStatus();
    }

    /**
     * 验证优惠券是否可用
     *
     * @param scope 使用场景
     */
    @Override
    public void verify(GCouponUserVo gCouponUser, String scope) {
        if (!gCouponUser.getStatusCode().equals("0"))
            throw new CustomException("优惠券已使用或已过期");
        if (!gCouponUser.getUseScopeCode().equals("0")) {
            if (!scope.equals(gCouponUser.getUseScopeCode()))
                throw new CustomException("此优惠券不能用于购买此商品");
        }
    }

    /**
     * 验证优惠券是否可用
     *
     * @param scopeList 使用场景
     */
    @Override
    public void verify(GCouponUserVo gCouponUser, List<String> scopeList) {
        if (!gCouponUser.getStatusCode().equals("0"))
            throw new CustomException("优惠券已使用或已过期");
        if (!gCouponUser.getUseScopeCode().equals("0")) {
            if (!scopeList.contains(gCouponUser.getUseScopeCode()))
                throw new CustomException("此优惠券不能用于购买此商品");
        }
    }

    @Override
    public void use(GCouponUserVo vo) {
        // todo 在调用支付接口后调用此方法
        GCouponUser gCouponUser = new GCouponUser();
        ToolUtils.copyPropertiesIgnoreNull(vo, gCouponUser);
        gCouponUser.setStatusCode("1");
        gCouponUser.setUseDate(new Date());
        this.updateGCouponUser(gCouponUser);
    }

    @Override
    public int countByUser(Long userId) {
        return gCouponUserMapper.countByUser(userId);
    }

}
